﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;

namespace BOW
{
    public partial class CreateBOW : Form
    {
        public CreateBOW()
        {
            InitializeComponent();
        }

    
        List<string> listWordofBug = new List<string>(); // danh sách toàn bộ bug
        
        Dictionary<string, double> BOW = new Dictionary<string, double>();
        
       

        ConnectDB connect = new ConnectDB();
        Const con = new Const();      
        Common commo = new Common();
        Calculator cal = new Calculator();

        //Dictionary<string, int> dicRemove = new Dictionary<string, int>();
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> temp = new Dictionary<string, int>();
            for (int i = 0; i < lvShow.Items.Count; i++)
            {
                if (lvShow.Items[i].Checked == true)
                {
                    temp.Add(lvShow.Items[i].Text, int.Parse(lvShow.Items[i].SubItems[1].Text));
                    lvShow.Items[i].Remove();
                    i--;
                }
            }
            foreach (string item in temp.Keys)
            {
                int vl = int.Parse(temp[item].ToString());
                ListViewItem showitem = new ListViewItem(item);
                ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, vl.ToString());
                showitem.SubItems.Add(sbitem);
                lvAdd.Items.Add(showitem);
                lvAdd.CheckBoxes = true;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> temp = new Dictionary<string, int>();
            for (int i = 0; i < lvAdd.Items.Count; i++)
            {
                if (lvAdd.Items[i].Checked == true)
                {
                    temp.Add(lvAdd.Items[i].Text, int.Parse(lvAdd.Items[i].SubItems[1].Text));
                    lvAdd.Items[i].Remove();
                    i--;
                }
            }
            foreach (string item in temp.Keys)
            {
                int vl = int.Parse(temp[item].ToString());
                ListViewItem showitem = new ListViewItem(item);
                ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, vl.ToString());
                showitem.SubItems.Add(sbitem);
                lvShow.Items.Add(showitem);
                lvShow.CheckBoxes = true;
            }
        }
        private void btnSeclectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lvShow.Items.Count; i++)
            {
                lvShow.Items[i].Checked = true;
                
            }
        }
        private void btnSeclectAllRemove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lvAdd.Items.Count; i++)
            {
                lvAdd.Items[i].Checked = true;

            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateBOW.ActiveForm.Close();
            //ShowFormat.ActiveForm.Activate();
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string File = openFileDialog1.FileName;
            if (string.IsNullOrEmpty(File))
                return;
            txtLink.Text = File;
            if (txtLink.Text != "")
                viewWord(txtLink.Text);
        }
        private void viewWord(string path)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            Dictionary<string, int> show = new Dictionary<string, int>();       
            List<int> getColumns = new List<int>();
            getColumns = commo.disField(path);
            DataTable test = connect.getDataFromXLS(path);
            string ArrayText = "";
            if (test != null)
            {
                for (int i = 0; i < test.Rows.Count; i++)
                {
                    for (int j = 0; j < test.Columns.Count; j++)
                    {
                        if (getColumns.Contains(j))
                            continue;  
                        ArrayText += test.Rows[i][j].ToString() + " ";
                    }
                }

                listWordofBug = commo.processWordofBug(ArrayText);

                foreach (string s in listWordofBug)
                {
                    string tem = s.ToUpper();
                    if (dic.ContainsKey(tem))
                    {
                        int val = int.Parse(dic[tem].ToString());
                        dic.Remove(tem);
                        dic.Add(tem, ++val);
                        dic[tem] = dic[tem]++;
                    }
                    else
                        dic.Add(tem, 1);
                }
               
                foreach (string item in dic.Keys)
                {
                    show.Add(item, int.Parse(dic[item].ToString()));
                    ListViewItem showitem = new ListViewItem(item.ToString());
                    ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, dic[item].ToString());
                    showitem.SubItems.Add(sbitem);
                    lvShow.Items.Add(showitem);
                }

                lvShow.CheckBoxes = true;
            }
            else
            {
                MessageBox.Show("Empty data");
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (lvAdd.Items.Count > 0)
            {
                Dictionary<string, double> wfile = new Dictionary<string, double>();
                Dictionary<string, double> bag = new Dictionary<string, double>();
                for (int i = 0; i < lvAdd.Items.Count; i++)
                {
                    string s = lvAdd.Items[i].Text;
                    int vl = int.Parse(lvAdd.Items[i].SubItems[1].Text);
                    wfile.Add(s, vl);
                }
                commo.WriteText(wfile);
                DataTable test = connect.getDataFromXLS(txtLink.Text);
                bag = commo.bag(test, wfile,txtLink.Text);
                commo.WriteBagOfWord(bag);

                lvAdd.Items.Clear();

                MessageBox.Show("Created successful");
            }
            else MessageBox.Show("Create fail");
        }

        

       
    }
}
